:- module(http_server).
:- export http_server / 2.
:- export http_server / 1.
:- comment(summary, "HTTP server library").
:- comment(author, "Ph. Bonnet, S. Bressan and M. Meier, ECRC Munich").
:- comment(copyright, "Imperial College London and ICL").
:- comment(date, "$Date: 2004/10/27 14:32:37 $").
:- comment(http_server / 1, [template : "http_server(+Port)", summary : "Start an http server", args : ["Method" : "An integer port number"], desc : html("\n    The server does: \n    <UL>\n    <LI> creation of a socket, bind it to current Host and given Port and listen \n    <LI> accept a connection on the socket \n    <LI> reception of a request \n    <LI> decoding of the request (method + url + http param init) \n    <LI> call the predicate http_method in module http_method \n    <LI> encoding of the response (depending on server function) \n    <LI> send the response on the socket \n    </UL>\n    NOTE:  The predicate http_server/1 requires that a module http_method\n    is defined that contains a predicate http_method/6.  This predicate is\n    used by the programmer to customize the server.  For instance the\n    method GET can be simply implemented.  The programmer can define its\n    own methods. \n    <P>\n    A simple example of server is the implementation of the method\n    GET.  A module is created that contains the predicate\n    http_method/6 that implements the method GET:  a read on the file\n    identified by its URL.  The file is returned if it is found,\n    otherwise an error parameter is returned. \n    <P>\n    This simple program can be used to test HTML pages.  Viewers such\n    as Netscape provide a view code option that signalizes syntax\n    errors in the HTML code.  This simple program can be used as a\n    light weight testing tool, possibly launched from the directory\n    where the HTML page resides. \n    "), eg : "\n    [eclipse 1]: [user].\n     \n    /********************************************************************\n     *  test (server)\n     *******************************************************************/\n\n    :- module(http_method).\n\n    :- set_error_handler(170, fail/0).\n    :- set_error_handler(171, fail/0).\n\n    /* \n    http_method(+Method, +Url, +ObjectBody, -Output, -StatusCode, -Parameter)\n    executes the method on the object and returns:\n    - the output of the method (possibly empty)\n    - a status code for the response status line\n    - a list of http parameters (in particular the length of the object body).\n\n    */\n\n\n    http_method(\"GET\", Url, _, Contents, 200, [contentLength(CL)]):-\n\t    append_strings(\"/\", FileName, Url),\n\t    getContents(FileName, Contents), !,\n\t    string_length(Contents, CL).\n    http_method(\"GET\", _, _, \"\", 404, []).\n\t    \n\n    getContents(Url, Contents):-\n\t    open(Url, read, s),\n\t    read_string(s, \"\", _, Contents),\n\t    close(s).\n\n    ^D\n\n    yes.\n\n    [eclipse 2]: use_module(http).\n    http_grammar.pl compiled traceable 25048 bytes in 0.27 seconds\n    http_client.pl compiled traceable 6052 bytes in 0.28 seconds\n    http_server.pl compiled traceable 5564 bytes in 0.03 seconds\n    http.pl    compiled traceable 0 bytes in 0.35 seconds\n\n    yes.\n    [eclipse 3]: use_module(http_method).\n\n    yes.\n    [eclipse 4]: http_server(8000).\n    "]).
:- comment(http_server / 2, [template : "http_server(+Port,+Pending)", summary : "Start an http server with allows the specified number of pending connections", args : ["Method" : "An integer port number", "Pending" : "The number of simultaneous connections to queue up before rejecting"], desc : html("<CODE>http_server/1</CODE> corresponds to a pending queue size of 1.")]).
